package com.agilestudio.ifinance.web.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.agilestudio.ifinance.entity.account.Menu;
import com.agilestudio.ifinance.web.AbstractController;
import com.agilestudio.modules.orm.Page;
import com.agilestudio.modules.orm.PropertyFilter;

@Controller
@RequestMapping(value = "menu")
public class MenuController extends AbstractController {

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Map<String, Object> map) {
		log.info("RequestMethod.GET menu list...");
		initMenus(map);
		return "menu/list";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public ModelAndView add(Map<String, Object> map) {
		log.info("RequestMethod.GET menu add...");
		initMenus(map);
		ModelAndView mav = new ModelAndView("menu/add");
		Menu menu = new Menu();
		mav.addObject("menu", menu);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView edit(Map<String, Object> map, @PathVariable String id) {
		log.info("RequestMethod.GET menu edit..." + id);
		initMenus(map);
		ModelAndView mav = new ModelAndView("menu/edit");
		Menu menu = accountService.getMenu(Long.valueOf(id));
		mav.addObject("menu", menu);
		return mav;
	}


	@RequestMapping(value = "list", method = RequestMethod.POST)
	public String list(Map<String, Object> map, WebRequest wr, Page<Menu> page) {
		for (Map.Entry<String, String[]> s : wr.getParameterMap().entrySet()) {
			log.info("list 0000------" + s.getKey() + ":"
					+ ToStringBuilder.reflectionToString(s.getValue()) + ":"
					+ s.getValue().length);
		}
		log.info("list ######## = " + page);
		initMenus(map);
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		page = accountService.searchMenu(page, filters);
		map.put("page", page);
		return "menu/list";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public ModelAndView save(Map<String, Object> map, @Valid Menu menu,
			BindingResult result) {
		initMenus(map);
		log.info("save = " + menu.toString());
		for (FieldError fe : result.getFieldErrors()) {
			log.info("save error FieldError= " + fe.getField() + ";"
					+ fe.getCode() + ";" + fe.getDefaultMessage());
		}
		ModelAndView mav = new ModelAndView("menu/edit");
		if (menu.getId() == null) {
			if (result.hasErrors()) {
				mav.setViewName("menu/add");
			} else {
				accountService.saveMenu(menu);
			}
			mav.addObject("menu", menu);
		} else {
			Menu u = accountService.getMenu(menu.getId());
			u.setName(menu.getName());
			u.setCode(menu.getCode());
			u.setLeaf(menu.isLeaf());
			u.setUrl(menu.getUrl());
			if (!result.hasErrors()) {
				accountService.saveMenu(u);
			}
			 mav.addObject("menu", u);
		}
		return mav;

	}

	@RequestMapping(value = "load", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, ? extends Object> load(@RequestParam("id") String id) {
		log.info("load = " + id);
		Menu menu = accountService.getMenu(Long.valueOf(id));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", menu.getId());
		log.info("load = " + map);
		return map;
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, String> delete(@RequestParam("id") String id) {
		log.info("delete1 = " + id);
		Menu menu = accountService.getMenu(Long.valueOf(id));
		menu.setUpdateUser("disabled");
		accountService.saveMenu(menu);
		Map<String, String> map = new HashMap<String, String>();
		map.put("info", "success");
		return map;
	}
}
